package juc;

import lombok.extern.slf4j.Slf4j;

//测试synchronized进行变量锁，完成正确的加减
@Slf4j(topic = "c.Test03")
public class Test03 {
    static int a = 0;
    static Object lock = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for(int i=0;i<5000;i++){
                synchronized (lock){
                    a++;
                    log.debug("t1的第{}次++",i);
                }
            }
        },"t1");
//        LockSupport.park(t1);
        Thread t2 = new Thread(() -> {
            for(int i=0;i<5000;i++){
                synchronized (lock){
                        a--;
                        log.debug("t2的第{}次--",i);
                }
            }
        },"t2");

        t1.start();
        t2.start();
        //这里需要加入join,需要等t1和t2一起结束，才能打印出来a
        t1.join();
        t2.join();

        log.debug("a的值为：{}",a);
    }


}
